package com.mail.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.api.SmsCouponApi;
import com.mail.dubbo.api.SmsCouponHistoryApi;
import com.mail.dubbo.api.SmsCouponProductCategoryRelationApi;
import com.mail.dubbo.api.SmsCouponProductRelationApi;
import com.mail.model.db.SmsCoupon;
import com.mail.model.db.SmsCouponHistory;
import com.mail.model.db.SmsCouponProductCategoryRelation;
import com.mail.model.db.SmsCouponProductRelation;
import com.mail.model.dto.SmsCouponDto;
import com.mail.model.vo.PageResult;
import com.mail.server.common.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yan
 * @date 2022/06/22 20:59
 **/
@Service
public class SmsCouponService {
    @DubboReference
    private SmsCouponApi smsCouponApi;
    @DubboReference
    private SmsCouponProductCategoryRelationApi smsCouponProductCategoryRelationApi;
    @DubboReference
    private SmsCouponProductRelationApi smsCouponProductRelationApi;

    public R create(SmsCouponDto smsCouponDto) {
        //塞数据
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(smsCouponDto, smsCoupon);
        SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = smsCouponDto.getSmsCouponProductCategoryRelation();
        SmsCouponProductRelation smsCouponProductRelation = smsCouponDto.getSmsCouponProductRelation();
        smsCouponApi.add(smsCoupon);
        if(smsCouponProductCategoryRelation!=null){
            smsCouponProductCategoryRelationApi.add(smsCouponProductCategoryRelation);
        }
        if(smsCouponProductRelation!=null){
            smsCouponProductRelationApi.add(smsCouponProductRelation);
        }
        return R.success(null);
    }

    public R delete(Long id) {
        smsCouponApi.delete(id);
        smsCouponProductRelationApi.delete(id);
        smsCouponProductCategoryRelationApi.delete(id);
        return R.success(null);
    }

    public R list(String name, Long pageNum, Long pageSize, Integer type) {
        SmsCoupon smsCoupon = new SmsCoupon();
        smsCoupon.setName(name);
        smsCoupon.setType(type);
        Page<SmsCoupon> list = smsCouponApi.list(pageNum, pageSize, smsCoupon);
        PageResult pageResult=new PageResult(pageNum,pageSize,list.getTotal(),list.getRecords());
        return R.success(pageResult);
    }

    public R update(Long id, SmsCouponDto couponDto) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(couponDto, smsCoupon);
        SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = couponDto.getSmsCouponProductCategoryRelation();
        SmsCouponProductRelation smsCouponProductRelation = couponDto.getSmsCouponProductRelation();
        smsCouponApi.update(smsCoupon);
        if(
                smsCouponProductCategoryRelation!=null
        ){
            smsCouponProductCategoryRelationApi.update(smsCouponProductCategoryRelation,id);
        }
        if(smsCouponProductRelation!=null){
            smsCouponProductRelationApi.update(smsCouponProductRelation,id);
        }
        return R.success(null);
    }

    public R getCouponById(Long id) {
        SmsCoupon smsCoupon =smsCouponApi.getById(id);
        SmsCouponProductRelation spr=smsCouponProductRelationApi.getCouponById(id);
        SmsCouponProductCategoryRelation scpcr=smsCouponProductCategoryRelationApi.getCouponById(id);
        SmsCouponDto couponDto =new SmsCouponDto();
        BeanUtils.copyProperties(smsCoupon,couponDto );
        couponDto.setSmsCouponProductRelation(spr);
        couponDto.setSmsCouponProductCategoryRelation(scpcr);
        return R.success(couponDto);
    }
}
